﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BiffenGUI.BiffenService;
using System.ServiceModel;

namespace BiffenGUI.UserControls
{
    public partial class BookingAdd : UserControl
    {
        private enum FieldState : int { Enabled = 1, Disabled = 0 }

        private BiffenServiceClient service = null;
        private List<Movie> movies = null;
        private List<Show> shows = null;
        private int selectedMovie = -1;
        private int selectedShow = -1;
        private List<int> seats = new List<int>();

        public BookingAdd()
        {
            InitializeComponent();
        }

        public void SetServiceReferance(BiffenServiceClient svc)
        {
            service = svc;
            ucSeats.ServiceClient = svc;
            Initialize();
        }

        public void UpdatePrice()
        {
            txtPhone.Focus();

            Movie m = (Movie) listMovies.Items[selectedMovie];
            lblPriceValue.Text = (m.PriceCategory.Price * ucSeats.GetAllSeats().Count).ToString();
        }

        private void CurrentMovies_Load(object sender, EventArgs e)
        {

        }

        private void Initialize()
        {
            listMovies.DisplayMember = "Title";
            listMovies.ValueMember = "Id";
            listShows.DisplayMember = "StartTime";
            listShows.ValueMember = "Id";

            movies = service.GetCurrentMovies();
            UpdateMovieLists();

        }

        private void UpdateMovieLists()
        {
            listMovies.Items.Clear();
            listMovies.Items.AddRange(movies.ToArray());

            listShows.Items.Clear();
        }

        private void ClearFields()
        {
            txtPhone.Text = "";
            chkSold.Checked = false;
        }

        private void SetFieldState(FieldState state)
        {
            bool s = !Convert.ToBoolean(state);
            //txtSeats.ReadOnly = s;
            txtPhone.ReadOnly = s;
            chkSold.Enabled = !s;
        }

        private void listMovies_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (listMovies.SelectedIndex != -1)
            {
                // Reset
                ucSeats.Clear();
                listShows.Items.Clear();
                bDone.Enabled = false;
                SetFieldState(FieldState.Disabled);
                ClearFields();
                selectedShow = -1;

                // Update
                selectedMovie = listMovies.SelectedIndex;
                Movie m = (Movie)listMovies.Items[selectedMovie];
                shows = service.GetShowsForMovie(m.Id);

                if (shows != null)
                {
                    listShows.Items.AddRange(shows.ToArray());
                }
            }
        }

        private void listShows_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (listShows.SelectedIndex != -1)
            {
                // Reset
                bDone.Enabled = true;
                ClearFields();
                SetFieldState(FieldState.Enabled);


                // Update
                selectedShow = listShows.SelectedIndex;
                Show s = (Show)listShows.Items[selectedShow];

                ucSeats.UpdateShow(s);

                UpdatePrice();
            }
        }

        private void bDone_Click(object sender, EventArgs e)
        {

            seats = ucSeats.GetAllSeats();

            int state = (int) Booking.States.Reserved;
            if (chkSold.Checked)
            {
                state = (int)Booking.States.Sold;
            }

            Show show = (Show)listShows.Items[selectedShow];
            try
            {
                int result = service.CreateBooking(state, int.Parse(txtPhone.Text), 1, show.Id, seats, 0);

                if (result > 0)
                {
                    MessageBox.Show("Booking tilføjet!");
                    ucSeats.UpdateShow(show);
                    ClearFields();
                    //txtSeats.Focus();
                }

                UpdatePrice();
            }
            catch (FaultException<SeatsAlreadyReservedFault>)
            {
                MessageBox.Show("Sæderne du forsøgte at reservere, er allerede reserveret.");
            }
            
        }

        private void txtPhone_KeyDown(object sender, KeyEventArgs e)
        {
            e.SuppressKeyPress = Tools.ValidateKeyPressAsNumber(e);
        }

    }
}
